//
//  OceanBaseTableVC.m
//  OceanStaffApp
//
//  Created by Zheng on 2019/7/31.
//  Copyright © 2019 Zh. All rights reserved.
//

#import "OceanBaseTableVC.h"


@interface OceanBaseTableVC ()

@end

@implementation OceanBaseTableVC

- (instancetype)init {
    self = [super init];
    if (self) {
        _tableViewStyle = UITableViewStyleGrouped;
        _lineEdgeInsets = UIEdgeInsetsMake(0, 12, 0, 0);
        _haveRefreshHeader = YES;
        _kEmptyTitle = kEmptyContentTitle;
        _kEmptyImgName = kEmptyBgImageName;
        _kEmptyBtnTitle = kEmptyBtnTitle;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view addSubview:self.tableView];
    [self makeSubviewsCreate];
    [self makeSubviewsLayout];
}

#pragma mark - RegisterCells
- (void)registerCells {
    [_tableView registerClass:[OceanBaseTableCell class] forCellReuseIdentifier:kCellID];
}

#pragma mark - LoadData
- (void)loadNetData {};

- (void)endLoadNetData {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.mj_header endRefreshing];
    });
}

#pragma mark - Subviews
- (void)makeSubviewsCreate {}

- (void)makeSubviewsLayout {
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:_tableViewStyle];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.emptyDataSetSource = self;
        _tableView.emptyDataSetDelegate = self;
        _tableView.separatorColor = CX_GetColorHex(@"#F5F5F5", 1);
        _tableView.rowHeight = 50.;
        _tableView.estimatedSectionFooterHeight = 0.;
        _tableView.estimatedSectionHeaderHeight = 0.;
        _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        [self registerCells];

        if (_haveRefreshHeader) {
            OceanBaseGifHeader *header = [OceanBaseGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNetData)];
            [header configLoadingImages];
            _tableView.mj_header = header;
        } else {
            _tableView.mj_header = nil;
        }

    }
    return _tableView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _modelsArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    OceanBaseTableCell *cell = [OceanBaseTableCell createTableViewCell:tableView indexPath:indexPath model:_modelsArr[indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (_tableView.separatorStyle == UITableViewCellSeparatorStyleNone) {
        return;
    }

    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:_lineEdgeInsets];
    }

    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:_lineEdgeInsets];
    }
}


#pragma mark - DZNEmptyDataSetSource
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSAttributedString *title = [[NSAttributedString alloc] initWithString:_kEmptyTitle attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]}];
    return title;
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIImage imageNamed:_kEmptyImgName inBundle:[NSBundle bundleForClass:[self class]] compatibleWithTraitCollection:nil];
}

- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView {
    return CX_GetColorHex(@"#F5F5F5", 1);;
}

- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
    NSDictionary *attribute = @{NSFontAttributeName:[UIFont systemFontOfSize:15],
                                NSForegroundColorAttributeName:[UIColor grayColor]};
    return [[NSAttributedString alloc] initWithString:_kEmptyBtnTitle attributes:attribute];
}

- (UIImage *)buttonBackgroundImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
    UIEdgeInsets capInsets = UIEdgeInsetsMake(10., 10., 10., 10.);
    UIEdgeInsets rectInsets = UIEdgeInsetsMake(-20., -CX_GetXScale(122), -20., -CX_GetXScale(122));
    UIImage *image = [UIImage imageNamed:kEmptyBtnImagaName];
    return [[image resizableImageWithCapInsets:capInsets resizingMode:UIImageResizingModeTile] imageWithAlignmentRectInsets:rectInsets];
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button {
    [_tableView.mj_header beginRefreshing];
}

@end
